package day06;

import java.io.*;

public class Demo11 {

    public static void main(String[] args) throws Exception {
        User user1 = new User(1, "TOM", 100, "北京");
        User user2 = new User(2, "Jerry", 100, "上海");
        //对象序列化
        ObjectOutputStream out=new ObjectOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream("demo.dat")
                )
        );
        out.writeObject(user1);
        out.writeObject(user2);
        out.close();
        //对象反序列化

        ObjectInputStream in = new ObjectInputStream(
                new BufferedInputStream(
                        new FileInputStream("demo.dat")
                )
        );
        while (true) {
            try {
                User user = (User) in.readObject();
                System.out.println(user);
            }catch (EOFException e){
                e.printStackTrace();
                System.out.println("到达末尾！");
                break;
            }
        }
        in.close();
    }
}
